package com.inspur.zhangpeixia.goods.servlet;

import com.inspur.zhangpeixia.goods.entity.Category;
import com.inspur.zhangpeixia.goods.entity.Good;
import com.inspur.zhangpeixia.goods.service.CategoryService;
import com.inspur.zhangpeixia.goods.service.GoodService;
import com.inspur.zhangpeixia.goods.service.impl.CategoryServiceImpl;
import com.inspur.zhangpeixia.goods.service.impl.GoodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "goodsServlet",value="/goods")
public class GoodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method=req.getParameter("method");
        if(method.equals("list")){
            listGoods(req,resp);
        }else if(method.equals("add")){
            addGoods(req,resp);
        }else if(method.equals("update")){
            updateGoods(req,resp);
        }else{
            deleteGoods(req,resp);

        }
    }

    private GoodService goodService=new GoodServiceImpl();
    private void listGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        req.setCharacterEncoding("UTF-8");
        String kw = req.getParameter("kw");//获取条件查询参数
        if (kw == null) {
            kw = "";
        }
        //转码，解决乱码
        kw = new String(kw.getBytes("ISO-8859-1"), "UTF-8");
//调用service中的ListCategory
        List<Good> list = goodService.listGoods(kw);
        System.out.println(list);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/pages/goods/goods_list.jsp").forward(req, resp);//转发到列表页面



    }



    private void deleteGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{

        String id = req.getParameter("id");
        int result = goodService.deleteGood(Integer.parseInt(id));
        if(result>0){
            listGoods(req,resp);
        }
    }

    private void updateGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        String goodsName = req.getParameter("goodsName");
        goodsName = new String(goodsName.getBytes("ISO-8859-1"),"UTF-8");

        // 获取并转换新增的价格、库存和分类ID参数
        double goodsPrice = Double.parseDouble(req.getParameter("goodsPrice"));
        int goodsStock = Integer.parseInt(req.getParameter("goodsStock"));
        int categoryId = Integer.parseInt(req.getParameter("categoryId")); // 新增分类ID处理

        Good good = new Good();
        good.setGoodsId(goodsId);
        good.setGoodsName(goodsName);
        good.setGoodsPrice(goodsPrice);
        good.setGoodsStock(goodsStock);
        good.setCategoryId(categoryId); // 设置分类ID

        int result = goodService.updateGood(good);
        if(result > 0){
            listGoods(req, resp);
        }
    }
    private void addGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //  req.setCharacterEncoding("UTF-8");
        // 获取前端页面提交的商品信息

        String goodsName = req.getParameter("goodName");
        goodsName = new String(goodsName.getBytes("ISO-8859-1"),"UTF-8");
        String categoryIdStr = req.getParameter("categoryId");
        String priceStr = req.getParameter("price");
        String stockStr = req.getParameter("stock");

        // 处理参数，转换类型
        int categoryId = Integer.parseInt(categoryIdStr);
        double goodsPrice = Double.parseDouble(priceStr);
        int goodsStock = Integer.parseInt(stockStr);

        // 创建 Good 实体类并设置属性
        Good good = new Good();
        good.setCategoryId(categoryId);
        good.setGoodsName(goodsName);
        good.setGoodsPrice(goodsPrice);
        good.setGoodsStock(goodsStock);

        // 调用 Service 层方法添加商品
        int result = goodService.addGood(good);
        if (result > 0) {
            // 添加成功，重定向或转发到商品列表页面，这里示例转发
            listGoods(req, resp);
        } else {
            // 可根据实际需求处理添加失败的情况，比如返回错误提示页面等
            req.setAttribute("errorMsg", "商品添加失败，请重试");
            req.getRequestDispatcher("/pages/goods/add_goods.jsp").forward(req, resp);
        }
    }

    }







